﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace GUI.PhieuChi // VƯƠNG TRÍ LỢI
{
    public partial class frmQuanLy : Form
    {

        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Delegate
        public void SelectData(string hanhdong)
        {
            try
            {
                dgvHienThi.DataSource = HienThi();
                hienthi = (Entities.PhieuChi[])dgvHienThi.DataSource;
                if (hanhdong == Common.Constants.Insert)
                {
                    frmXuLy XuLyPhieuChi = new frmXuLy(Common.Constants.Insert);
                    XuLyPhieuChi.Select = new frmXuLy.SelectData(SelectData);
                    XuLyPhieuChi.Show();
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Khởi tạo

        bool checkClose = false;
        public Entities.PhieuChi[] hienthi;
        private Entities.PhieuChi phieuchi;

        DateTime batdau;
        DateTime ketthuc;
        #endregion

        #region Form Quan Lý Phiếu Chi

        public frmQuanLy()
        {
            try
            {
                InitializeComponent();
                dgvHienThi.DataSource = HienThi();
                hienthi = (Entities.PhieuChi[])dgvHienThi.DataSource;
                HienThiList();
                Event();
            }
            catch 
            { }
          }

        // When enter on datagridview
        private void Event()
        {
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
            rdbMaHoaDon.Checked = true;
        }
        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                for (int i = 0; i < hienthi.Length; i++)
                {
                    if (hienthi[i].MaPhieuChi == dgvHienThi.CurrentRow.Cells["MaPhieuChi"].Value.ToString())
                    {
                        phieuchi = hienthi[i];
                        this.Xem();
                    }
                }
            }
        }

        public void frmQuanLyPhieuChi_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if (!checkClose)
                    if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        e.Cancel = checkClose;
                    }
                    else
                        e.Cancel = !checkClose;
                else
                {
                }
            }
            catch
            {
            }
        }

        public Entities.PhieuChi[] HienThi()
        {
            try
            {
                Entities.PhieuChi[] phieuchi = Common.StaticVariable.InterfaceObject.SelectPhieuChiTheoDiaDiem(Entitystatic.ViewFile().MaKho.ToString());
               
                Entities.PhieuChi[] temp = new Entities.PhieuChi[phieuchi.Length];
                int sl = 0;
                for (int i = 0; i < phieuchi.Length; i++)
                {
                    if (phieuchi[i].Deleted == false)
                    {
                        temp[sl] = phieuchi[i];
                        sl++;
                    }
                }
                phieuchi = new Entities.PhieuChi[sl];
                for (int i = 0; i < phieuchi.Length; i++)
                {
                    phieuchi[i] = temp[i];
                }
                //lbTongBanGhi.Content = Common.Constants.TongSo + phieuchi.Length;
                return phieuchi;
            }
            catch { return new Entities.PhieuChi[0]; }
        }

        private void HienThiList()
        {
            try
            {
                if (hienthi == null)
                {
                    Entities.PhieuChi gitri = new Entities.PhieuChi();
                    dgvHienThi.DataSource = gitri;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["MaPhieuChi"].HeaderText = "Mã Phiếu Chi";
                dgvHienThi.Columns["MaPhieuChi"].Visible = true;
                dgvHienThi.Columns["KhoanMuc"].HeaderText = "Khoản Mục";
                dgvHienThi.Columns["KhoanMuc"].Visible = true;
                dgvHienThi.Columns["DoiTuong"].HeaderText = "Đối Tượng";
                dgvHienThi.Columns["DoiTuong"].Visible = true;
                dgvHienThi.Columns["NguoiNhanTien"].HeaderText = "Người Nhận Tiền";
                dgvHienThi.Columns["NguoiNhanTien"].Visible = true;
                dgvHienThi.Columns["TongTienThanhToan"].HeaderText = "Tổng Tiền";
                dgvHienThi.Columns["TongTienThanhToan"].Visible = true;
                dgvHienThi.Columns["GhiChu"].HeaderText = "Ghi chú";
                dgvHienThi.Columns["GhiChu"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
               
            }
            catch
            { }
        }
        #endregion

        #region Check Permission
        public bool CheckPer(int hanhDong)
        {
            //return Common.CheckQuyen.Check(Common.FormName.PhieuChi, hanhDong);
            return true;
        }
        #endregion

        #region Nghiệp vụ
        // thêm
        public void Them()
        {
            try
            {
                // check per
                if (!CheckPer(1))
                {
                    MessageBox.Show(Common.MSG.MSG10(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check loop
                {
                    frmXuLy XuLyPhieuChi = new frmXuLy(Common.Constants.Insert);
                    XuLyPhieuChi.Select = new frmXuLy.SelectData(SelectData);
                    XuLyPhieuChi.Show();
                }
            }
            catch
            {
            }
        }

        // Xem
        public void Xem()
        {
            try
            {    
                // check per
                if (!CheckPer(0))
                {
                    MessageBox.Show(Common.MSG.MSG9(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                for (int i = 0; i < hienthi.Length; i++)
                {
                    if (hienthi[i].MaPhieuChi == dgvHienThi.CurrentRow.Cells["MaPhieuChi"].Value.ToString())
                    {
                        phieuchi = hienthi[i];
                    }
                }
                // check object
                if (phieuchi == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe);
                    return;
                }
                    frmXuLy XuLyPhieuChi = new frmXuLy(phieuchi, Common.Constants.Update);
                    XuLyPhieuChi.Select = new frmXuLy.SelectData(SelectData);
                    XuLyPhieuChi.Show();
                
                    dgvHienThi.DataSource = HienThi();

            }
            catch
            {
            }
        }

        void XuLy()
        {
            ArrayList array = new ArrayList();
            foreach (Entities.PhieuChi item in hienthi)
            {
                if (item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc.Date)
                {
                    array.Add(item);
                }
            }
            hienthi = (Entities.PhieuChi[])array.ToArray(typeof(Entities.PhieuChi));
        }

        void LocDK()
        {
            try
            {
                GUI.LocDieuKien.frmLocDieuKien frm = new LocDieuKien.frmLocDieuKien();
                frm.ShowDialog();
                if (GUI.LocDieuKien.frmLocDieuKien.kiemtra)
                {
                    DateTime truoc1;
                    DateTime sau1;
                    try
                    {
                        truoc1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.truoc);
                        sau1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.sau);
                    }
                    catch
                    {
                        return;
                    }
                    batdau = truoc1;
                    ketthuc = sau1;
                    hienthi = HienThi();
                    XuLy();
                    dgvHienThi.DataSource = hienthi;
                    GUI.LocDieuKien.frmLocDieuKien.kiemtra = false;
                }
            }
            catch
            {
            }
        }


        public bool CheckConflicDelete()
        {
            try
            {
                Entities.PhieuChi[] temp = HienThi();
                for (int i = 0; i < temp.Length; i++)
                {
                    if (phieuchi.MaPhieuChi == temp[i].MaPhieuChi)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        //in
        public void In()
        {
            try
            {
                for (int i = 0; i < hienthi.Length; i++)
                {
                    if (hienthi[i].MaPhieuChi == dgvHienThi.CurrentRow.Cells["MaPhieuChi"].Value.ToString())
                    {
                        phieuchi = hienthi[i];
                    }
                }
                // check object
                if (phieuchi == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe);
                    return;
                }
                this.Enabled = false;
                BaoCao.frmBaoCaorpt frm = new BaoCao.frmBaoCaorpt(phieuchi);
                if (frm.kiemtra)
                {
                    frm.ShowDialog();
                    frm.kiemtra = false;
                }
                this.Enabled = true;
            }
            catch
            {
            }
        }

        // Thoát
        public void Thoat()
        {
            try
            {
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    checkClose = true;
                    this.Close();
                }
            }
            catch
            {
            }
        }

        #endregion

        #region Sự kiện của windown

        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            try
            {
                //
                this.Closing += new System.ComponentModel.CancelEventHandler(frmQuanLyPhieuChi_Closing);
                this.WindowState = FormWindowState.Maximized;
                //Select firts row
                if (dgvHienThi.RowCount != 0)
                {
                    phieuchi = hienthi[0];
                }

                // click chuột trái image
                btnThoat.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnThem.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnXem.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnIn.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnLocDieuKien.MouseDown += new MouseEventHandler(MouseDown_Chung);

                //select changed
                dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
                dgvHienThi.MouseDoubleClick += new MouseEventHandler(dgvHienThi_MouseDoubleClick);

                

                // text changed
                txttimkiem.TextChanged += new EventHandler(txttimkiem_TextChanged);
                //
                lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
            }
            catch 
            { }
        }

        private void frmQuanLy_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
            }
            catch 
            { }
        }

        // Event SelectChange of listview
        public void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                for (int i = 0; i < hienthi.Length; i++)
                {
                    if (hienthi[i].MaPhieuChi == dgvHienThi.CurrentRow.Cells["MaPhieuChi"].Value.ToString())
                    {
                        phieuchi = hienthi[i];
                    }
                }
            }
            catch { }
        }

        //lvphieuchi_MouseDoubleClick
        public void dgvHienThi_MouseDoubleClick(object sender, MouseEventArgs e)     
        {
            try
            {
                Xem();
            }
            catch { }
        }

        // Search data in database following display on listview
        public void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int soluong = 0;
                if (rdbMaHoaDon.Checked == true)
                {
                    for (int i = 0; i < hienthi.Length; i++)
                    {
                        int kiemtra = hienthi[i].MaPhieuChi.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper());
                        if (kiemtra >= 0)
                        {
                            soluong++;
                        }
                    }
                    if (soluong == 0)
                    {
                        dgvHienThi.DataSource = new Entities.PhieuChi[0];
                        //lbTongBanGhi.Content = Common.Constants.TongSo + "0";
                        return;
                    }
                    Entities.PhieuChi[] hienthitheoid = new Entities.PhieuChi[soluong];
                    soluong = 0;
                    for (int i = 0; i < hienthi.Length; i++)
                    {
                        int kiemtra = hienthi[i].MaPhieuChi.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper());
                        if (kiemtra >= 0)
                        {
                            hienthitheoid[soluong] = hienthi[i];
                            soluong++;
                        }
                    }
                    dgvHienThi.DataSource = hienthitheoid;
                    //lbTongBanGhi.Content = Common.Constants.TongSo + hienthitheoid.Length;
                }
            }
            catch { }
        }

        #endregion

        #region Mouse Down Chung

        public void MouseDown_Chung(object sender, MouseEventArgs e)
        {
            try
            {
                Button temp = (Button)sender;
                switch (temp.Name)
                {
                    case Common.Constants.btnThoat:
                        {
                            Thoat();
                            break;
                        }
                    case Common.Constants.btnThem:
                        {
                            Them();
                            break;
                        }
                    case Common.Constants.btnXem:
                        {
                            Xem();
                            break;
                        }
                    case Common.Constants.btnIn:
                        {
                            In();
                            break;
                        }
                    case Common.Constants.btnLocDieuKien:
                        {
                            LocDK(); 
                            break;
                        }
                    default: break;
                }
            }
            catch 
            {
            }
        
        }

        #endregion

    }
}
